package com.idp.app.controller;

import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.idp.app.service.AppLoginService;
import com.idp.common.base.BaseController;
import com.idp.common.model.LoginInfo;
import com.idp.common.model.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * APP登录Controller
 * @author king
 */
@Api(tags="APP登录")
@RestController
@RequestMapping("/appLogin/")
public class AppLoginController extends BaseController {

	@Autowired
	private AppLoginService appLoginService;

	/**
	 * 账号密码登录
	 * @param loginParams
	 * @return
	 */
	@ApiOperation("账号密码登录")
	@DynamicParameters(name = "loginParams", properties = {
			@DynamicParameter(name = "uname", value = "用户名", dataTypeClass = String.class),
			@DynamicParameter(name = "pwd", value = "密码", dataTypeClass = String.class),
			@DynamicParameter(name = "deviceInfo", value = "登录设备信息", dataTypeClass = String.class)
	})
	@PostMapping("loginPwd")
	public ResultModel<LoginInfo> loginPwd(@RequestBody Map<String, String> loginParams) {
		String uname = loginParams.get("uname");
		String pwd = loginParams.get("pwd");
		String deviceInfo = loginParams.get("deviceInfo");
		return ResultModel.success(appLoginService.loginPwd(uname, pwd, deviceInfo));
	}

	/**
	 * 微信登录
	 * @param loginParams
	 * @return
	 */
	@ApiOperation("微信登录")
	@DynamicParameters(name = "loginParams", properties = {
			@DynamicParameter(name = "code", value = "微信用户标识", dataTypeClass = String.class),
			@DynamicParameter(name = "phoneCode", value = "手机验证码", dataTypeClass = String.class),
			@DynamicParameter(name = "deviceInfo", value = "登录设备信息", dataTypeClass = String.class)
	})
	@PostMapping("loginWechat")
	public ResultModel<LoginInfo> loginWechat(@RequestBody Map<String, String> loginParams) {
		String code = loginParams.get("code");
		String phoneCode = loginParams.get("phoneCode");
		String deviceInfo = loginParams.get("deviceInfo");
		return ResultModel.success(appLoginService.loginWechat(code, deviceInfo, phoneCode));
	}

	/**
	 * 微信静默登录
	 * @param loginParams
	 * @return
	 */
	@ApiOperation("微信静默登录")
	@DynamicParameters(name = "loginParams", properties = {
			@DynamicParameter(name = "code", value = "微信用户标识", dataTypeClass = String.class),
			@DynamicParameter(name = "deviceInfo", value = "登录设备信息", dataTypeClass = String.class)
	})
	@PostMapping("loginWechatAuthBase")
	public ResultModel<LoginInfo> loginWechatAuthBase(@RequestBody Map<String, String> loginParams) {
		String code = loginParams.get("code");
		String deviceInfo = loginParams.get("deviceInfo");
		return ResultModel.success(appLoginService.loginWechatAuthBase(code, deviceInfo));
	}

	/**
	 * 获取短信验证码
	 * @param smsParams
	 * @return
	 */
	@ApiOperation("获取短信验证码")
	@DynamicParameters(name = "smsParams", properties = {
			@DynamicParameter(name = "phone", value = "手机号", dataTypeClass = String.class)
	})
	@PostMapping("getSmsCode")
	public ResultModel getSmsCode(@RequestBody Map<String, String> smsParams) {
		String phone = smsParams.get("phone");
		appLoginService.getSmsCode(phone);
		return ResultModel.success();
	}

	/**
	 * 校验手机验证码
	 * @param smsParams
	 * @return
	 */
	@ApiOperation("校验手机验证码")
	@DynamicParameters(name = "smsParams", properties = {
			@DynamicParameter(name = "phone", value = "手机号", dataTypeClass = String.class),
			@DynamicParameter(name = "code", value = "手机验证码", dataTypeClass = String.class),
	})
	@PostMapping("checkSmsCode")
	public ResultModel<LoginInfo> checkSmsCode(@RequestBody Map<String, String> smsParams) {
		String phone = smsParams.get("phone");
		String code = smsParams.get("code");
		return ResultModel.success(appLoginService.checkSmsCode(phone, code));
	}

	/**
	 * 短信验证码登录
	 * @param loginParams
	 * @return
	 */
	@ApiOperation("短信验证码登录")
	@DynamicParameters(name = "smsParams", properties = {
			@DynamicParameter(name = "phone", value = "手机号", dataTypeClass = String.class),
			@DynamicParameter(name = "code", value = "手机验证码", dataTypeClass = String.class),
			@DynamicParameter(name = "deviceInfo", value = "登录设备信息", dataTypeClass = String.class)
	})
	@PostMapping("loginSms")
	public ResultModel<LoginInfo> loginSms(@RequestBody Map<String, String> loginParams) {
		String phone = loginParams.get("phone");
		String code = loginParams.get("code");
		String deviceInfo = loginParams.get("deviceInfo");
		return ResultModel.success(appLoginService.loginSms(phone, code, deviceInfo));
	}

}
